package com.example.admin_oa.config;

import com.example.admin_oa.shiro.AccoutRealm;
import com.example.admin_oa.shiro.JwtFilter;
import com.example.admin_oa.shiro.MyCreadentialsMatcher;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author cjl
 */
@Configuration
public class ShiroConfig {
    @Autowired
    private MyCreadentialsMatcher myCreadentialsMatcher;
    @Autowired
    private AccoutRealm accoutRealm;

    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        accoutRealm.setCredentialsMatcher(myCreadentialsMatcher);
        securityManager.setRealm(accoutRealm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean=new ShiroFilterFactoryBean();
        Map<String, Filter> map=new LinkedHashMap<>();
        map.put("authc",new JwtFilter());
        factoryBean.setFilters(map);
        Map<String,String> filterMap=new LinkedHashMap<>();
        //允许swagger通过
        filterMap.put("/swagger-ui.html**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/adminOa/User/login", "anon");
        filterMap.put("/adminOa/menu/getMenu", "anon");
        filterMap.put("adminOa/User/captcha","anon");
        filterMap.put("/**", "authc");
        filterMap.put("http://localhost:1013/**", "authc");
        factoryBean.setFilterChainDefinitionMap(filterMap);
        factoryBean.setSecurityManager(securityManager);
        return factoryBean;
    }

    @Bean
    public AccoutRealm accoutRealm(){
        return new AccoutRealm();
    }

    @Bean
    public JwtFilter getJwtFilter(){
        return new JwtFilter();
    }

    // 开启注解代理
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager")DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
//        creator.setProxyTargetClass(true);
//        return creator;
//    }
}